package com.xposed.xinhao.hook_jingdong;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.xposed.xinhao.utils.JsonUtils;
import com.xposed.xinhao.utils.LogUtils;

import java.io.BufferedReader;
import java.io.File;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

/**
 * 采源宝V3.5.1
 */
public class JingdongHook {

    //保存上下文
    private Context mContext;

    private XC_LoadPackage.LoadPackageParam lpparam;

    public JingdongHook(XC_LoadPackage.LoadPackageParam lpparam) {
        this.lpparam = lpparam;
    }

    public void hookMethod() {
//        XposedHelpers.findAndHookMethod(ClassLoader.class, "loadClass", String.class, new XC_MethodHook() {
//            @Override
//            protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
//                if(param.args != null && param.args[0] != null && param.args[0].toString().startsWith("de.robv.android.xposed.")){
//                    // 改成一个不存在的类
//                    param.args[0] = "de.robv.android.xposed.ThTest";
//                    LogUtils.e("--------------------");
//                }
//                super.beforeHookedMethod(param);
//            }
//        });
        XposedHelpers.findAndHookMethod(BufferedReader.class, "readLine", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                String result = (String) param.getResult();
                if (result != null) {
                    if (result.contains("/data/data/de.robv.android.xposed.installer/bin/XposedBridge.jar")) {
                        param.setResult("");
                        new File("").lastModified();
                    }
                }

                super.afterHookedMethod(param);
            }
        });
        XposedHelpers.findAndHookMethod(Activity.class, "onCreate", Bundle.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(final MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                mContext = (Context) param.thisObject;
                LogUtils.e("Activity onCreate：" + mContext.getClass().getName());
                if (mContext.getClass().getName().equals("com.jingdong.app.mall.main.MainActivity"))
                    XposedHelpers.findAndHookMethod("com.jingdong.app.mall.hotfix.tinker.c", mContext.getClassLoader(), "g", Throwable.class, new XC_MethodHook() {
                        @Override
                        protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
                            super.beforeHookedMethod(param);
                            LogUtils.e("------------");
                        }

                        @Override
                        protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                            super.afterHookedMethod(param);
                            LogUtils.e("------------" + param.getResult());
                        }
                    });
            }
        });

    }
}
